package com.dyp.stompwebsocket.resource;

import com.dyp.stompwebsocket.entity.ChatRequest;
import com.dyp.stompwebsocket.entity.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;

import java.io.IOException;

@Controller
public class TestResource {

    @Autowired
    public SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/send/to/all")
    @SendTo("/subscribeTest")
    public ChatResponse mess(ChatRequest chatRequest){
        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setContent(chatRequest.getContent());
        chatResponse.setFromUserId(chatRequest.getFromUserId());
        chatResponse.setContent(chatRequest.getContent());
        return chatResponse;
    }

    @MessageMapping("/single")
    public ChatResponse single(ChatRequest chatRequest){
        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setContent(chatRequest.getContent());
        chatResponse.setFromUserId(chatRequest.getFromUserId());
        chatResponse.setContent(chatRequest.getContent());
        this.simpMessagingTemplate.convertAndSendToUser("/topic/user/"+chatResponse.getToUserId(),"/single",chatResponse);
        return chatResponse;
    }

    @SubscribeMapping("/subscribeTest")
    public ChatResponse sub(){
        return new ChatResponse();
    }

}
